package org.easy.http.client.cookie;

import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;

import java.util.List;
import java.util.Objects;

/**
 * @Description TODO
 * @Author WuXueLi
 * @Date 15:46 2023/2/4
 * @Version 1.0
 **/
public class EasyDefaultCookieJar implements CookieJar {

    private EasyCookieStore cookieStore;

    public EasyDefaultCookieJar(EasyCookieStore cookieStore) {
        if (Objects.isNull(cookieStore)) {
            throw new NullPointerException("CookieStore may not be null.");
        }
        this.cookieStore = cookieStore;
    }

    @Override
    public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
        cookieStore.add(httpUrl, list);
    }

    @Override
    public List<Cookie> loadForRequest(HttpUrl httpUrl) {
        return cookieStore.get(httpUrl);
    }
}
